//
//  ShopViewController.m
//  cardmenNew
//
//  Created by Dengzeyu on 16/1/1.
//  Copyright © 2016年 Dengzeyu. All rights reserved.
//

#import "ShopViewController.h"
#import "jingtai.h"
#include "MyView.h"
#import "GetURLContent.h"
#import "FVCustomAlertView.h"
#import "UIImage_imageScale.h"
#import "M_shop.h"
#import "UIDeviceMeasure1.h"
@interface ShopViewController ()
{
    
}
@property (weak, nonatomic) IBOutlet UIBarButtonItem *downloaditem;
@property(nonatomic,retain)UIView * aa;
@property (weak, nonatomic) IBOutlet UIBarButtonItem *Settingitem;
@end

@implementation ShopViewController
- (IBAction)hasbeendownload:(id)sender {
    ContentViewController * ck ;
    UIDeviceMeasure1 *device = [[UIDeviceMeasure1 alloc]init];
    switch ([device identifyResolution ]) {
        case UIDevice_iPad:
        {
            NSLog(@"ipad");
            UIStoryboard *storyBoard=[UIStoryboard storyboardWithName:@"content" bundle:nil];
            ck=[storyBoard instantiateViewControllerWithIdentifier:@"ipad"];
            
            break;
            
        }
            
        case UIDevice_iPhone4_4s:
        {
            NSLog(@"iphone4");
            UIStoryboard *storyBoard=[UIStoryboard storyboardWithName:@"content" bundle:nil];
            ck=[storyBoard instantiateViewControllerWithIdentifier:@"iphone4"];
            
            break;
            
        }
        case UIDevice_iPhone5_5s:
        {
            NSLog(@"ip5");
            UIStoryboard *storyBoard=[UIStoryboard storyboardWithName:@"content" bundle:nil];
            ck=[storyBoard instantiateViewControllerWithIdentifier:@"iphone5"];
            
            break;
        }
        case UIDevice_iPhone6:
        {
            NSLog(@"ip6");
            UIStoryboard *storyBoard=[UIStoryboard storyboardWithName:@"content" bundle:nil];
            ck=[storyBoard instantiateViewControllerWithIdentifier:@"iphone6"];
            
            break;
        }
        case UIDevice_iPhone6plus:
        {
            NSLog(@"ip6p");
            UIStoryboard *storyBoard=[UIStoryboard storyboardWithName:@"content" bundle:nil];
            ck=[storyBoard instantiateViewControllerWithIdentifier:@"ChoseViewcontroller"];
            
            break;
        }
            
    }
    ck.kindname= [jingtai sharedSingleton].shopview.kind;
    ck.leixing =1;
    [self presentViewController:ck animated:YES completion:nil];

}

- (IBAction)gotosetting:(id)sender {
}

- (void)viewDidLoad {
 
    [super viewDidLoad];
    _appList = [[NSMutableArray alloc] init];
    self.Settingitem.image = [[UIImage imageNamed:@"setting"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
     self.downloaditem.image =[[UIImage imageNamed:@"history"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    self.title = @"商家";
    CGRect rectStatus = [[UIApplication sharedApplication] statusBarFrame];
    self.myTopic = [[DZYTopic alloc] init];
    NSLog(@"width view %f",self.myTopic.frame.size.width);
    CGRect topicrect =CGRectMake(0,rectStatus.size.height+self.navigationController.navigationBar.frame.size.height, self.view.frame.size.width, self.view.frame.size.width*0.4375);
    self.myTopic.frame =topicrect;
    NSLog(@"width view %f",self.myTopic.frame.size.width);
    CGRect pagerect =CGRectMake(0, self.myTopic.frame.size.height+rectStatus.size.height+self.navigationController.navigationBar.frame.size.height, self.view.frame.size.width, self.myTopic.frame.size.height*0.1);
    self.page = [[UIPageControl alloc] init];
    [self.page setFrame:pagerect];

    self.topicArray = [[NSMutableArray alloc] init];
    [self.topicArray addObject:[NSDictionary dictionaryWithObjects:@[@"http://163.54114.com/upimg/allimg/120619/5-120619112512.jpg" ,@"PIC3",@NO,@"http://www.yunyankeji.com"] forKeys:@[@"pic",@"title",@"isLoc",@"URL"]]];
    //网络图片加载失败
    [self.topicArray addObject:[self.topicArray objectAtIndex:0]];
    [self.topicArray addObject:[self.topicArray objectAtIndex:0]];
    
    NSLog(@"%@",self.topicArray);
    //加入数据
    [self.myTopic setSelf];
    self.myTopic.pics = self.topicArray;
    [self.myTopic upDate];
    
    self.myTopic.clipsToBounds = YES;
    self.myTopic.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
    [self.page setNumberOfPages:self.topicArray.count];
    self.page.currentPage = 0;
    self.page.currentPageIndicatorTintColor = [UIColor blackColor];
    self.page.pageIndicatorTintColor = [UIColor grayColor];
    [self.view addSubview:self.myTopic];
    [self.view addSubview: self.page];
    
    CGRect myrect = CGRectMake(0, self.myTopic.frame.size.height+self.page
                               .frame.size.height+self.navigationController.navigationBar.frame.size.height+rectStatus.size.height, self.view.frame.size.width, self.view.frame.size.height-self.myTopic.frame.size.height-self.page.frame.size.height-self.navigationController.navigationBar.frame.size.height-rectStatus.size.height-self.tabBarController.tabBar.frame.size
                               .height-self.myTopic.frame.size.height/3.0);
    self.myMainscroll = [[UIScrollView alloc]initWithFrame:myrect];
    [self.view addSubview:self.myMainscroll];
    self.serchButton =[[UIButton alloc] init];
    CGRect searchButtonRect = CGRectMake(0, self.myMainscroll.frame.origin.y+self.myMainscroll.frame.size.height, self.view.frame.size.width, self.myTopic.frame.size.height/3.0);
    self.serchButton.frame =searchButtonRect;
    UIImage * searchimage = [UIImage imageNamed:@"search"];
    searchimage = [searchimage TransformtoSize:CGSizeMake(self.myMainscroll.frame.size.width/2, self.serchButton.frame.size.height)];
    [self.serchButton setImage:searchimage forState:UIControlStateNormal];
    [self.view addSubview:self.serchButton];
    
    
    if (![[jingtai sharedSingleton].shopversion isEqualToString:[jingtai getlocalshopversion]]) {
        self.aa  = [FVCustomAlertView showDefaultLoadingAlertOnView:self.view withTitle:@"正在加载数据"];
    }
  dispatch_queue_t queue =  dispatch_queue_create("loadshop", nil);
    dispatch_sync(queue, ^{
        NSString * url =[[jingtai sharedSingleton].mainIP stringByAppendingString:@"/cardmennew/getshop.aspx?kind="];
        url = [url stringByAppendingString:self.kind];
        GetURLContent * geturl = [[GetURLContent alloc] init];
        [geturl Load:url andshopview:self];
        dispatch_async(dispatch_get_main_queue(), ^{
             [self getzannum];
            [self loadscroll];
        });
    });
        [jingtai sharedSingleton].shopview = self;
   
   }
-(void)viewDidAppear:(BOOL)animated
{
   
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)getzannum
{
    for(int i=0;i<self.appList.count;i++)
    {
        NSString * shopname =((M_shop *)[self.appList objectAtIndex:i]).name;
        NSString * kindname = self.kind;
        NSString * url = [NSString stringWithFormat:@"%@/cardmennew/getkindID.aspx?kind=%@&shop=%@",[jingtai sharedSingleton].mainIP,self.kind,shopname];
        NSString * kindID = [[[GetURLContent alloc] init] getkindID:url];
        NSString * urls = [NSString stringWithFormat:@"%@/cardmennew/getzannum.aspx?kindid=%@&shop=%@&kind=%@",[jingtai sharedSingleton].mainIP,kindID,shopname ,kindname];
        
        NSString * str = [[[GetURLContent alloc] init] getzannum:urls];

        ((M_shop *)[self.appList objectAtIndex:i]).zan =str;
    }
}
-(void) loadscroll
{
    int totalColumns = 4;
    
   
    CGFloat Y =0;
    CGFloat W = self.myMainscroll.frame.size.width/4.0;
    CGFloat H = self.myMainscroll.frame.size.height/4.0;
    CGFloat X = (self.view.frame.size.width - totalColumns * W) / (totalColumns + 1);
    for ( int index = 0; index <_appList.count ; index++) {
        NSLog(@"indes %d",index);
        Model *model = _appList[index];
        int row = index / totalColumns;
        int col = index % totalColumns;
        CGFloat viewX = X + col * (W + X);
        
        CGFloat viewY = 0 + row * (H + Y);
  

            NSLog(@"for duoxiancheng %d",index);
            MyView *view = [[MyView alloc] initWithFrame:CGRectMake(viewX, viewY, W, H) Model:model MyButtonBlock:^{
                
                //            _myLabel = [[MyLabel alloc] init];
                //
                //            [self.view addSubview:_myLabel];
                
                NSLog(@"block");
            }];
           
            view.model = model;
            [self.myMainscroll addSubview:view];
            [[NSRunLoop currentRunLoop] runMode: NSDefaultRunLoopMode beforeDate: [NSDate date]];
                          CGFloat maxY = CGRectGetMaxY(view.frame);
                          self.myMainscroll.contentSize = CGSizeMake(0, maxY);

      
        
               //_scroview.contentSize =  CGSizeMake(0, 266);
        
    }
    if(self.aa != nil)
    [self.aa removeFromSuperview];
    [jingtai setlocalshopVersion:[jingtai sharedSingleton].shopversion];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
